//
//  PickerCityView.h
//  DanaKreditLoan
//
//  Created by ZhengChengFeng on 2024/10/22.
//

#import "PickerCityView.h"
#import "PickerCityTableCell.h"

#define kTabCell   @"kTabCell"

#define kTabCellHeight  40

@interface PickerCityView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UIView *wholeView,*bvv;

@property (nonatomic) UITableView *tableView;

@property (nonatomic) NSArray *arr;

@property (nonatomic) NSMutableArray *dataOneArr,*dataTwoArr,*dataThreeArr;

@property (nonatomic) UIButton *btn1,*btn2,*btn3;

@property (nonatomic) NSInteger btnTag;

@property (nonatomic) NSString *str1,*str2,*str3;

@property (nonatomic) UIImageView *tipIconImgView;

@property (nonatomic) UIView *lineV1,*lineV2,*lineV3;

@end

@implementation PickerCityView

- (instancetype)initWithFrame:(CGRect)frame withTitleStr:(NSString *)titleStr withDataArr:(NSArray *)arr{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kShadowViewColor;
        
        self.dataOneArr = [[NSMutableArray alloc] init];
        self.dataTwoArr = [[NSMutableArray alloc] init];
        self.dataThreeArr = [[NSMutableArray alloc] init];
        
        CGFloat kHeight = 25 + 25 + 10 + 40 + 10 + 10 + 48  + (IS_IPhoneX_All ? kHeight_BottomSafe : 15) + 200;
                
        self.arr = arr;
        
        self.wholeView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kHeight)];
        self.wholeView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.wholeView];
        self.wholeView.layer.mask = [NSString UIBezierPathWithRect:self.wholeView.bounds withCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadiu:CGSizeMake(14, 14)];
        
        UILabel *titleLab = [UILabel createLabel:[UIFont fontWithName:kFuturaBold size:15] color:kBlackColor];
        [self.wholeView addSubview:titleLab];
        titleLab.textAlignment  = 1;
        titleLab.frame = CGRectMake(0, 25, kScreenWidth, 25);
        titleLab.text = esString(titleStr);
        
        UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 10 - 20, 10, 20, 20)];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_cancel"] forState:UIControlStateNormal];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_cancel"] forState:UIControlStateSelected];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_cancel"] forState:UIControlStateHighlighted];
        [cancelBtn addTarget:self action:@selector(clickCacel) forControlEvents:UIControlEventTouchUpInside];
        [self.wholeView addSubview:cancelBtn];
        
        UIView *bvv = [[UIView alloc] initWithFrame:CGRectMake(0, titleLab.bottom + 10, kScreenWidth, 40)];
        bvv.backgroundColor = [UIColor whiteColor];
        [self.wholeView addSubview:bvv];
        self.bvv = bvv;
        
        [self initBtn];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(30, bvv.bottom - 1, kScreenWidth - 60, 1)];
        line.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
        [self.wholeView addSubview:line];
        
        

        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(30,bvv.bottom + 10,kScreenWidth - 60,200) style:UITableViewStylePlain];
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.showsVerticalScrollIndicator = NO;
        [self.wholeView addSubview:self.tableView];
        
        [self.tableView registerClass:[PickerCityTableCell class] forCellReuseIdentifier:kTabCell];
        
        self.dataOneArr = [PickerCityModel mj_objectArrayWithKeyValuesArray:arr];
        
        [self.tableView reloadData];

        UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, self.wholeView.height - 48 - (IS_IPhoneX_All ? kHeight_BottomSafe : 15), kScreenWidth - 60, 48)];
        confirmBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:16];
        [confirmBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        [confirmBtn setTitleColor:kWhiteColor forState:UIControlStateSelected];
        [confirmBtn setTitleColor:kWhiteColor forState:UIControlStateHighlighted];
        [confirmBtn setTitle:ASLocalizedString(@"Confirming") forState:UIControlStateNormal];
        [confirmBtn setTitle:ASLocalizedString(@"Confirming") forState:UIControlStateSelected];
        [confirmBtn setTitle:ASLocalizedString(@"Confirming") forState:UIControlStateHighlighted];
        [confirmBtn setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateNormal];
        [confirmBtn setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateSelected];
        [confirmBtn setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateHighlighted];
        [confirmBtn addTarget:self action:@selector(clickConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.wholeView addSubview:confirmBtn];
        confirmBtn.layer.cornerRadius = 24;
        confirmBtn.layer.masksToBounds = YES;
        [confirmBtn setImgRightTitltLeft:10];
        confirmBtn.backgroundColor = kButtonDefaultColor;
        
    }
    return self;
}

- (void)initBtn{
    
    CGFloat btnH = 40;
    CGFloat btnW = (kScreenWidth - 30 - 10 - 15 - 15)/3;
    
    self.btn1 = [[UIButton alloc] init];
    self.btn1.frame = CGRectMake(30, 0, btnW, btnH);
    [self.btn1 setTitle:ASLocalizedString(@"Choose") forState:UIControlStateNormal];
    [self.btn1 setTitleColor:kBlackColor forState:UIControlStateNormal];
    self.btn1.titleLabel.font = [UIFont fontWithName:kFutura size:14];
    [self.bvv addSubview:self.btn1];
    self.btn1.contentHorizontalAlignment = 1;
    
    self.lineV1 = [[UIView alloc] initWithFrame:CGRectMake(self.btn1.left + 18, 46, 14, 4)];
    self.lineV1.backgroundColor = kWhiteColor;
    [self.bvv addSubview:self.lineV1];
    
    self.btn2 = [[UIButton alloc] init];
    self.btn2.frame = CGRectMake(self.btn1.right, 0, btnW, btnH);
    [self.btn2 setTitle:ASLocalizedString(@"Choose") forState:UIControlStateNormal];
    [self.btn2 setTitleColor:kBlackColor forState:UIControlStateNormal];
    self.btn2.titleLabel.font = [UIFont fontWithName:kFutura size:14];
    [self.bvv addSubview:self.btn2];
    self.btn2.contentHorizontalAlignment = 1;
    
    self.lineV2 = [[UIView alloc] initWithFrame:CGRectMake(self.btn2.left + 18, 46, 14, 4)];
    self.lineV2.backgroundColor = kWhiteColor;
    [self.bvv addSubview:self.lineV2];
    
    self.btn3 = [[UIButton alloc] init];
    self.btn3.frame = CGRectMake(self.btn2.right, 0, btnW, btnH);
    [self.btn3 setTitle:ASLocalizedString(@"Choose") forState:UIControlStateNormal];
    [self.btn3 setTitleColor:kBlackColor forState:UIControlStateNormal];
    self.btn3.titleLabel.font = [UIFont fontWithName:kFutura size:14];
    [self.bvv addSubview:self.btn3];
    self.btn3.contentHorizontalAlignment = 1;
    
    self.lineV3 = [[UIView alloc] initWithFrame:CGRectMake(self.btn3.left + 18, 46, 14, 4)];
    self.lineV3.backgroundColor = kWhiteColor;
    [self.bvv addSubview:self.lineV3];
    
    self.btn1.tag = 0;
    [self.btn1 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btn2.tag = 1;
    [self.btn2 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btn3.tag = 2;
    [self.btn3 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.lineV1.hidden = NO;
    self.lineV2.hidden = YES;
    self.lineV3.hidden = YES;
    
    if (![[CurrentUser mine] isYNLanguage]) {
        self.btn3.hidden = YES;
        self.lineV3.hidden =YES;
    }
    
}

- (void)clickBtn:(UIButton *)btn{
    
    if ([btn.titleLabel.text isEqualToString:ASLocalizedString(@"Choose")]) {
        return;
    }
    
    if (![btn.titleLabel.text isEqualToString:ASLocalizedString(@"Choose")]) {
        
        if (btn.tag == 0) {
            
            self.str2 = @"";
            self.str3 = @"";
            
            self.lineV1.hidden = NO;
            self.lineV2.hidden = YES;
            self.lineV3.hidden = YES;
                    
            [self.tableView reloadData];
            
            [self.btn2 setTitle:ASLocalizedString(@"Choose") forState:UIControlStateNormal];
            [self.btn2 setTitleColor:kBlackColor forState:UIControlStateNormal];
            
            [self.btn3 setTitle:ASLocalizedString(@"Choose") forState:UIControlStateNormal];
            [self.btn3 setTitleColor:kBlackColor forState:UIControlStateNormal];
            
        }
        else if (btn.tag == 1){
            self.str3 = @"";
            
            self.lineV1.hidden = YES;
            self.lineV2.hidden = NO;
            self.lineV3.hidden = YES;
                    
            
            [self.tableView reloadData];
            
            [self.btn3 setTitle:ASLocalizedString(@"Choose") forState:UIControlStateNormal];
            [self.btn3 setTitleColor:kBlackColor forState:UIControlStateNormal];
            
        }
    }
    
    if (![[CurrentUser mine] isYNLanguage]) {
        self.btn3.hidden = YES;
        self.lineV3.hidden =YES;
    }
    

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kTabCellHeight;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (![[CurrentUser mine] isYNLanguage]) {
        if (!self.lineV1.hidden) {
            return self.dataOneArr.count;
        }
        else {
            return self.dataTwoArr.count;
        }
    }
    
    if (!self.lineV1.hidden) {
        return self.dataOneArr.count;
    }
    else if (!self.lineV2.hidden) {
        return self.dataTwoArr.count;
    }

    return self.dataThreeArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PickerCityTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kTabCell];
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (!self.lineV1.hidden) {
        
        cell.model = self.dataOneArr[indexPath.row];
        
        if ([self.btn1.titleLabel.text isEqualToString:cell.model.hydrogen]) {
            cell.titleLab.textColor = kButtonDefaultColor;
        } else {
            cell.titleLab.textColor = kBlackColor;
        }
    }

    else if (![[CurrentUser mine] isYNLanguage]) {
        cell.model = self.dataTwoArr[indexPath.row];
        
        if ([self.btn2.titleLabel.text isEqualToString:cell.model.hydrogen]) {
            cell.titleLab.textColor = kButtonDefaultColor;
        } else {
            cell.titleLab.textColor = kBlackColor;
        }
    }
    
    else if (!self.lineV2.hidden) {
        cell.model = self.dataTwoArr[indexPath.row];
        
        if ([self.btn2.titleLabel.text isEqualToString:cell.model.hydrogen]) {
            cell.titleLab.textColor = kButtonDefaultColor;
        } else {
            cell.titleLab.textColor = kBlackColor;
        }
    }
    else{
        
        if (self.dataThreeArr.count > 0) {
            cell.model = self.dataThreeArr[indexPath.row];
            
            if ([self.btn3.titleLabel.text isEqualToString:cell.model.hydrogen]) {
                cell.titleLab.textColor = kButtonDefaultColor;
            } else {
                cell.titleLab.textColor = kBlackColor;
            }
        }
        
        
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PickerCityTableCell *selCell = [tableView cellForRowAtIndexPath:indexPath];
    
    PickerCityModel *mm  = selCell.model;
    
    NSArray *nextArr = mm.valid;
    
    if (![[CurrentUser mine] isYNLanguage]) {
        self.btn3.hidden = YES;
        self.lineV3.hidden =YES;
    }
    
    if (self.lineV1.hidden == NO) {
    
        self.str1 = esString(mm.hydrogen);
        [self.btn1 setTitle:self.str1 forState:UIControlStateNormal];
        [self.btn1 setTitleColor:[UIColor colorWithHexString:@"#6C6C6C"] forState:UIControlStateNormal];

        self.lineV1.hidden = YES;
        self.lineV2.hidden = NO;
        self.lineV3.hidden = YES;
        
        [self.btn2 setTitleColor:kButtonDefaultColor forState:UIControlStateNormal];
        
        self.dataTwoArr = [PickerCityModel mj_objectArrayWithKeyValuesArray:nextArr];
    
    }
  
    else  if (self.lineV2.hidden == NO){
        self.str2 = esString(mm.hydrogen);
        [self.btn2 setTitle:self.str2 forState:UIControlStateNormal];
        [self.btn2 setTitleColor:[UIColor colorWithHexString:@"#6C6C6C"] forState:UIControlStateNormal];
        
        self.lineV1.hidden = YES;
        self.lineV2.hidden = YES;
        self.lineV3.hidden = NO;
        [self.btn3 setTitleColor:kButtonDefaultColor forState:UIControlStateNormal];
        
        
        
        if (![[CurrentUser mine] isYNLanguage]) {
            [self.dataThreeArr removeAllObjects];
            self.lineV3.hidden = YES;
        }
        else{
            self.dataThreeArr = [PickerCityModel mj_objectArrayWithKeyValuesArray:nextArr];
        }
        
    }
  
    else  if (self.lineV3.hidden == NO){
        self.str3 = esString(mm.hydrogen);
        [self.btn3 setTitle:self.str3 forState:UIControlStateNormal];
        [self.btn3 setTitleColor:[UIColor colorWithHexString:@"#6C6C6C"] forState:UIControlStateNormal];
        self.lineV1.hidden = YES;
        self.lineV2.hidden = YES;
        self.lineV3.hidden = YES;
    }
    
    else{
        if (![[CurrentUser mine] isYNLanguage]) {
            self.str2 = esString(mm.hydrogen);
            [self.btn2 setTitle:self.str2 forState:UIControlStateNormal];
            [self.btn2 setTitleColor:[UIColor colorWithHexString:@"#6C6C6C"] forState:UIControlStateNormal];
        }
        else{
            self.str3 = esString(mm.hydrogen);
            [self.btn3 setTitle:self.str3 forState:UIControlStateNormal];
            [self.btn3 setTitleColor:[UIColor colorWithHexString:@"#6C6C6C"] forState:UIControlStateNormal];
            self.lineV1.hidden = YES;
            self.lineV2.hidden = YES;
            self.lineV3.hidden = YES;
        }
        
    }
    

    [self.tableView reloadData];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]init];
    return vv;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]init];
    return vv;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001f;
}

- (void)clickConfirmBtn{
    
    if ([CurrentUser mine].isYNLanguage) {
        if (PMStringIsEmpty(self.str1) || PMStringIsEmpty(self.str2) || PMStringIsEmpty(self.str3)) {
            [BoxLoadingHelper showBoxWithMessage:ASLocalizedString(@"AddressTips")];
            return;
        }
    } else {
        if (PMStringIsEmpty(self.str1) || PMStringIsEmpty(self.str2)) {
            [BoxLoadingHelper showBoxWithMessage:ASLocalizedString(@"AddressTips")];
            return;
        }
    }
    
    
    [self clickCacel];
    
    NSString *cityStr = [NSString stringWithFormat:@"%@-%@",esString(self.str1),esString(self.str2)];
    if ([CurrentUser mine].isYNLanguage) {
        cityStr = [NSString stringWithFormat:@"%@-%@-%@",esString(self.str1),esString(self.str2),esString(self.str3)];
    }
    if (_callbackCityStr) {
        _callbackCityStr(cityStr);
    }
    
}

- (void)showBottomView{
    CGFloat kHeight = 25 + 25 + 10 + 40 + 10 + 10 + 48  + (IS_IPhoneX_All ? kHeight_BottomSafe : 15) + 200;
    
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        
        weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight - kHeight, kScreenWidth, kHeight);
         
     } completion:^(BOOL finished) {}];
    
}

- (void)clickCacel{
    CGFloat kHeight = 25 + 25 + 10 + 40 + 10 + 10 + 48  + (IS_IPhoneX_All ? kHeight_BottomSafe : 15) + 200;
    
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        
         weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kHeight);
         
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
